package com.vic.zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;



import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class TestZk {

    public static String url ="192.168.1.123:2181,192.168.1.124:2181,192.168.1.125:2181";
    public static int timeout=5000;
    static CountDownLatch countDownLatch  = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException {
            ZooKeeper zk = new ZooKeeper(url, timeout, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    //获取事件的状态
                    Event.KeeperState state = event.getState();
                    Event.EventType type = event.getType();
                    //如果建立连接
                    if(Event.KeeperState.SyncConnected==state){
                        //如果建立连接
                        if(Event.EventType.None==type){
                            countDownLatch.countDown();

                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println("连接已经建立");
                        }
                    }
                }
            });

            countDownLatch.await();

            Thread.sleep(50000);

            zk.create()

            zk.close();
    }
}

